package cn.itsource.pethome.system.logininfo.controller;

import cn.itsource.pethome.system.logininfo.domain.LoginRO;
import cn.itsource.pethome.system.logininfo.service.ILogininfoService;
import cn.itsource.pethome.system.logininfo.domain.Logininfo;
import cn.itsource.pethome.system.logininfo.query.LogininfoQuery;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
public class LogininfoController {
    @Autowired
    public ILogininfoService logininfoService;

    //账号登录 手机号  邮箱  用户名
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginRO login){
        try {
            login.setAutoLogin(true);
            return logininfoService.account(login);
        }catch (BusinessException e){
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请稍后在试！！！");
        }
    }

    /**
     * 保存和修改公用的
     * @param logininfo  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if( logininfo.getId()!=null)
                logininfoService.update(logininfo);
            else
                logininfoService.add(logininfo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                logininfoService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setSuccess(false).setMsg("操作失败");
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Logininfo findnoe = logininfoService.findnoe(id);
            return AjaxResult.success().setData(findnoe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Logininfo> list(){

        return logininfoService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult findQuey(@RequestBody LogininfoQuery query)
    {
        try {
            List<Logininfo> queryData = logininfoService.queryData(query);
            Long count = logininfoService.queryCount(query);
            return AjaxResult.success().setData(queryData).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
